package net.hn.hnms.biz.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.hn.hnms.biz.project.domain.ConstructionTask;
import net.hn.hnms.biz.project.mapper.ConstructionTaskMapper;
import net.hn.hnms.biz.project.request.ConstructionTaskReq;
import net.hn.hnms.biz.project.service.IConstructionTaskService;
import net.hn.hnms.biz.project.vo.ConstructionTaskVo;
import net.hn.hnms.sys.common.core.domain.R;
import net.hn.hnms.sys.common.core.domain.ResponsePage;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 刘昊
 * @date 2024-03-14 9:21
 */
@Service
@Slf4j
public class ConstructionTaskServiceImpl implements IConstructionTaskService {

    @Autowired
    private ConstructionTaskMapper constructionTaskMapper;
    @Override
    public R<Void> insertReq(ConstructionTaskReq req) {
        ConstructionTask taskDO = new ConstructionTask();
        try {
            BeanUtils.copyProperties(req,taskDO);
            Calendar startDateCalendar = Calendar.getInstance();
            startDateCalendar.setTime(taskDO.getTaskStartDate());
            Calendar endDateCalendar = Calendar.getInstance();
            endDateCalendar.setTime(taskDO.getTaskEndDate());
            long diffInMillis = endDateCalendar.getTimeInMillis() - startDateCalendar.getTimeInMillis();
            int days = (int) (diffInMillis / (1000 * 60 * 60 * 24));
            taskDO.setTaskTime(Integer.toString(days));
            log.info("施工任务新增========>");
            constructionTaskMapper.insert(taskDO);
        }catch (Exception e) {
            log.error("错误信息:{}",e,e);
            return R.fail("新增施工任务失败");
        }
        return R.ok("新增成功");
    }

    @Override
    public R<Void> deleteReq(ConstructionTaskReq req) {
        return R.ok("暂未提供删除方法");
    }

    @Override
    public R<Void> updateReq(ConstructionTaskReq req) {
        return R.ok("暂未提供更新方法");
    }

    @Override
    public R<List<ConstructionTaskVo>> selectList(ConstructionTaskReq req) {
        QueryWrapper<ConstructionTask> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(req.getMineCode())) {
            wrapper.eq("mine_code",req.getMineCode());
        }
        if (StringUtils.isNotBlank(req.getConstructionId())) {
            wrapper.eq("construction_id",req.getConstructionId());
        }else {
            return R.fail("关联计划id不能为空");
        }
        try {
            log.info("施工任务查询========>");
            List<ConstructionTaskVo> resultList = constructionTaskMapper.selectList(wrapper).stream()
                    .map(item -> {
                        ConstructionTaskVo res = new ConstructionTaskVo();
                        BeanUtils.copyProperties(item,res);
                        return res;
                    }).collect(Collectors.toList());
            return R.ok("查询完成",resultList);
        }catch (Exception e) {
            log.error("错误信息:{}",e,e);
            return R.fail("查询失败");
        }
    }

    @Override
    public R<Void> deleteList(List<String> idList) {
        return R.ok("暂未提供删除方法");
    }

    @Override
    public R<ResponsePage<List<ConstructionTaskVo>>> selectPageList(ConstructionTaskReq req, PageQuery pageQuery) {

        QueryWrapper<ConstructionTask> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(req.getMineCode())) {
            wrapper.eq("mine_code",req.getMineCode());
        }
        if (StringUtils.isNotBlank(req.getConstructionId())) {
            wrapper.eq("construction_id",req.getConstructionId());
        }else {
            return R.fail("关联计划id不能为空");
        }
        try {
            log.info("施工任务查询========>");
            Page<ConstructionTask> page = constructionTaskMapper.selectPage(pageQuery.build(),wrapper);
            List<ConstructionTaskVo> resPage = page.getRecords()
                    .stream()
                    .map(item -> {
                        ConstructionTaskVo res = new ConstructionTaskVo();
                        BeanUtils.copyProperties(item,res);
                        return res;
                    }).collect(Collectors.toList());
            return R.ok("查询完成",new ResponsePage<>(resPage, page.getTotal()));
        }catch (Exception e) {
            log.error("错误信息:{}",e,e);
            return R.fail("查询失败");
        }
    }
}
